package com.huawei.petapp2.fragment;


import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.huawei.petapp2.R;
import com.huawei.petapp2.activity.LoginActivity;
import com.huawei.petapp2.activity.OrderListActivity;
import com.huawei.petapp2.activity.widget.wrapper.BadgeWrapper;
import com.huawei.petapp2.address.manage.ManageAddressActivity;
import com.huawei.petapp2.collect.CollectActivity;
import com.huawei.petapp2.help.HelpActivity;
import com.huawei.petapp2.network.UserManager;
import com.huawei.petapp2.network.entity.User;
import com.huawei.petapp2.network.event.UserEvent;
import com.huawei.petapp2.settings.SettingsActivity;
import com.huawei.petapp2.utils.PreferenceHelper;
import com.huawei.petapp2.view.CircleImage;

import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的页面.
 */
public class MineFragment extends BaseFragment{

    public static com.huawei.petapp2.fragment.MineFragment newInstance() {
        return new com.huawei.petapp2.fragment.MineFragment();
    }

    @BindView(R.id.text_username)
    TextView tvName;

    @BindView(R.id.text_wait_pay)
    TextView tvWaitPay;
    @BindView(R.id.text_wait_ship)
    TextView tvWaitShip;
    @BindView(R.id.text_shipped)
    TextView tvShipped;
    @BindView(R.id.text_history)
    TextView tvHistory;
    @BindView(R.id.image_avatar)
    CircleImage imageAvatar;

    private BadgeWrapper mWaitPayBadge;
    private BadgeWrapper mWaitShipBadge;
    private BadgeWrapper mShippedBadge;

    TextView mineName;
    String username;
    String imgurl;
    Float balance;
    Integer userid;

    @Override
    public void initView(View mView) {
        mWaitPayBadge = new BadgeWrapper(tvWaitPay);
        mWaitShipBadge = new BadgeWrapper(tvWaitShip);
        mShippedBadge = new BadgeWrapper(tvShipped);
        username= PreferenceHelper.getString("username",getContext());
        imgurl=PreferenceHelper.getString("img",getContext());
        balance=PreferenceHelper.getFloat("balance",getContext());
        userid=PreferenceHelper.getInt("userid",getContext());
        Glide.with(mView)
                .load(imgurl)
                .dontAnimate()
                .into(imageAvatar);
        onEvents(userid);
    }

    private void onEvents(int userid) {
        System.out.println("本地保存的id"+userid);
        if (userid!=0) {
            User user = UserManager.getInstance().getUser();
            tvName.setText(PreferenceHelper.getString("username",getContext()));
            mWaitPayBadge.showNumber(1);//先用假的数据
            mWaitShipBadge.showNumber(1);
            mShippedBadge.showNumber(1);
        } else {
            tvName.setText(R.string.mine_sign_in_or_sign_up);
            mWaitPayBadge.hide();
            mWaitShipBadge.hide();
            mShippedBadge.hide();
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mine;
    }

    protected int getContentViewLayout() {
        return R.layout.fragment_mine;
    }


    protected void initView() {
        mWaitPayBadge = new BadgeWrapper(tvWaitPay);
        mWaitShipBadge = new BadgeWrapper(tvWaitShip);
        mShippedBadge = new BadgeWrapper(tvShipped);

    }


    @Override
    public void onEvent(UserEvent event) {
        super.onEvent(event);
        System.out.println("本地保存的id"+PreferenceHelper.getInt("userId", Objects.requireNonNull(getContext())));
        if (PreferenceHelper.getInt("userId", Objects.requireNonNull(getContext()))!=0) {
            User user = UserManager.getInstance().getUser();
            tvName.setText(PreferenceHelper.getString("username",getContext()));
            mWaitPayBadge.showNumber(1);//先用假的数据
            mWaitShipBadge.showNumber(1);
            mShippedBadge.showNumber(1);
        } else {
            tvName.setText(R.string.mine_sign_in_or_sign_up);
            mWaitPayBadge.hide();
            mWaitShipBadge.hide();
            mShippedBadge.hide();
        }
    }

    @OnClick({R.id.text_username, R.id.text_manage_address, R.id.text_wait_pay, R.id.text_order_unconfirmed,
            R.id.text_wait_ship, R.id.text_shipped, R.id.text_history, R.id.text_favorite,})
    void onClick(View view) {
        System.out.println("我的界面id号"+userid);
        if (userid==0) {
            Intent intent = new Intent(getContext(), LoginActivity.class);
            getActivity().startActivity(intent);
            return;
        }
        switch (view.getId()) {
            case R.id.text_username:
                break;
            case R.id.text_manage_address:
                Intent manageAddress = new Intent(getContext(), ManageAddressActivity.class);
                startActivity(manageAddress);
                break;
            case R.id.text_wait_pay://待付款
                Intent waitPay = OrderListActivity
                        .getStartIntent(getContext(), 1);
                startActivity(waitPay);
                break;
            case R.id.text_order_unconfirmed://带确定
                Intent unconfirmed = OrderListActivity
                        .getStartIntent(getContext(),0);
                startActivity(unconfirmed);
                break;
            case R.id.text_wait_ship://代发货
                Intent waitShip = OrderListActivity
                        .getStartIntent(getContext(), 2);
                startActivity(waitShip);
                break;
            case R.id.text_shipped://待评价
                Intent shipped = OrderListActivity
                        .getStartIntent(getContext(), 4);
                startActivity(shipped);
                break;
            case R.id.text_history://已完成
                Intent history = OrderListActivity
                        .getStartIntent(getContext(), 5);
                startActivity(history);
                break;
            case R.id.text_favorite:
                Intent collect = new Intent(getContext(), CollectActivity.class);
                startActivity(collect);
                break;
            default:
                throw new UnsupportedOperationException();
        }
    }

    @OnClick(R.id.button_setting)
    void navigateToSettings() {
        Intent intent = new Intent(getContext(), SettingsActivity.class);
        getActivity().startActivity(intent);
    }

    @OnClick(R.id.text_help)
    void navigateToHelp() {
        Intent intent = new Intent(getContext(), HelpActivity.class);
        getActivity().startActivity(intent);
    }


}
